package com.xmg.p2p.business.service.impl;

import com.xmg.p2p.base.domain.Account;
import com.xmg.p2p.base.util.BidConst;
import com.xmg.p2p.business.domain.*;
import com.xmg.p2p.business.mapper.AccountFlowMapper;
import com.xmg.p2p.business.service.IAccountFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @BelongsProject: jinrong-website
 * @BelongsPackage: com.xmg.p2p.business.service.impl
 * @author: Andy
 * @date: 2021.4.8 13:33
 * @Description: 流水信息的实现类
 * @since JDK 1.8
 */
@Service
public class IAccountFlowServiceImpl implements IAccountFlowService {
    @Autowired
    private AccountFlowMapper accountFlowMapper;

    // 基本的流水信息
    private AccountFlow baseFolw(Account account) {
        // 创建流水对象
        AccountFlow flow = new AccountFlow();
        flow.setVdate(new Date());  // 产生流水的业务时间
        flow.setUseableAmount(account.getUsableAmount()); // 已经在账户中更新过了可用金额
        flow.setFreezedAmount(account.getFreezedAmount()); // 已经在账户中更新过了冻结金额
        flow.setAccountId(account.getId());  // 设置当前流水对用的账号信息
        return flow;
    }
    /**
     * 添加一条线下充值单流水信息
     *
     * @param account   用户对象
     * @param ro    线下充值单
     */public void rechargeFlow(Account account, RechargeOffline ro) {
        AccountFlow flow = baseFolw(account);
        flow.setAccountActionType(BidConst.ACCOUNT_ACTIONTYPE_RECHARGE_OFFLINE);
        //设置此次对应的流水类型 : 线下充值
        flow.setAmount(ro.getAmount()); // 流水金额
        flow.setNote("线下充值成功, 充值金额为:" + ro.getAmount());
        this.accountFlowMapper.insert(flow);
    }

    @Override
    public void bidFlow(Account account, Bid bid) {
        // 创建流水对象
        AccountFlow flow = baseFolw(account);
        flow.setAccountActionType(BidConst.ACCOUNT_ACTIONTYPE_BID_FREEZED);
        //设置此次对应的流水类型 : 线下充值
        flow.setAmount(bid.getAvailableAmount()); // 流水金额
        flow.setNote("投标:" + bid.getBidRequestTitle()+"的借款，投标的冻结金额:"+bid.getAvailableAmount());
        this.accountFlowMapper.insert(flow);
    }

    @Override
    public void cance1BidFlow(Account account, Bid bid) {
        AccountFlow flow = baseFolw(account);
        flow.setAccountActionType(BidConst.ACCOUNT_ACTIONTYPE_BID_UNFREEZED);
        flow.setAmount(bid.getAvailableAmount());
        //设置本次流水金额
        flow.setNote("投标" + bid.getBidRequestTitle() +
                "的借款,借款失败,取消冻结金额:" + bid.getAvailableAmount());
        this.accountFlowMapper.insert(flow);
    }

    @Override
    public void borrowSuccessFlow(Account borrowAccount, BidRequest bidRequest) {
        AccountFlow flow = baseFolw(borrowAccount);
        flow.setAccountActionType(BidConst.ACCOUNT_ACTIONTYPE_BIDREQUEST_SUCCESSFUL); //成功借款
        flow.setAmount(bidRequest.getBidRequestAmount());   // 设置金额为借款的总金额
        flow.setNote("借款" +
                bidRequest.getTitle() + "成功,收到借款金额:" +
                bidRequest.getBidRequestAmount());
        this.accountFlowMapper.insert(flow);

    }

    @Override
    public void bidSuccessFlow(Account bidAccount, Bid bid) {
        AccountFlow flow = baseFolw(bidAccount);
        flow.setAccountActionType(BidConst.ACCOUNT_ACTIONTYPE_BID_SUCCESSFUL);
        flow.setAmount(bid.getAvailableAmount());
        flow.setNote("投标" + bid.getBidRequestTitle() +
                "成功,取消投标冻结金额:" + bid.getAvailableAmount());
        this.accountFlowMapper.insert(flow);

    }

    @Override
    public void returnMoneyFlow(Account returnAccount, PaymentSchedule ps) {
        AccountFlow flow = baseFolw(returnAccount);
        flow.setAccountActionType(BidConst.ACCOUNT_ACTIONTYPE_RETURN_MONEY);
        flow.setAmount(ps.getTotalAmount());
        flow.setNote("借款" + ps.getBidRequestTitle() + "第" + ps.getMonthIndex() + "期成功还款!");
        this.accountFlowMapper.insert(flow);
    }

    @Override
    public void receiveMoneyFlow(Account bidAccount, PaymentScheduleDetail psd) {
        AccountFlow flow = baseFolw(bidAccount);
        flow.setAccountActionType(BidConst.ACCOUNT_ACTIONTYPE_CALLBACK_MONEY);
        flow.setAmount(psd.getTotalAmount());
        flow.setNote("借款第" + psd.getMonthIndex() + "期成功收款!");
        this.accountFlowMapper.insert(flow);

    }

    @Override
    public void interestChargeFeeFlow(Account bidAccount, BigDecimal interestChargeFee, PaymentScheduleDetail psd) {
        AccountFlow flow = baseFolw(bidAccount);
        flow.setAccountActionType(BidConst.ACCOUNT_ACTIONTYPE_INTEREST_SHARE);
        flow.setAmount(interestChargeFee);
        flow.setNote("借款第" + psd.getMonthIndex() + "期成功收款!支付利息管理费" + interestChargeFee);
        this.accountFlowMapper.insert(flow);

    }

    @Override
    public void whitdrawAppllyFlow(Account account, MoneyWithdraw m) {
        AccountFlow flow = baseFolw(account);
        flow.setAccountActionType(BidConst.ACCOUNT_ACTIONTYPE_WITHDRAW_FREEZED);
        flow.setAmount(m.getAmount());
        flow.setNote("提现申请 ,冻结金额:" + m.getAmount());
        this.accountFlowMapper.insert(flow);

    }

    @Override
    public void moneyWithdrawSuccessFlow(Account account, MoneyWithdraw m) {
        AccountFlow flow = baseFolw(account);
        flow.setAccountActionType(BidConst.ACCOUNT_ACTIONTYPE_WITHDRAW);
        flow.setAmount(m.getAmount().subtract(m.getChargeFee()));
        flow.setNote("提现成功,取消冻结金额:" + flow.getAmount());
        this.accountFlowMapper.insert(flow);
    }

    @Override
    public void moneyWithdrawChargeFeeFlow(Account account, MoneyWithdraw m) {
        AccountFlow flow = baseFolw(account);
        flow.setAccountActionType(BidConst.ACCOUNT_ACTIONTYPE_WITHDRAW_MANAGE_CHARGE);
        flow.setAmount(m.getChargeFee());
        flow.setNote("提现成功,支付提现手续费:" + m.getChargeFee());
        this.accountFlowMapper.insert(flow);
    }

    @Override
    public void cancleWithdrawFlow(Account account, MoneyWithdraw m) {
        AccountFlow flow = baseFolw(account);
        flow.setAccountActionType(BidConst.ACCOUNT_ACTIONTYPE_WITHDRAW_UNFREEZED);
        flow.setAmount(m.getAmount());
        flow.setNote(m.getApplier().getUsername() + "提现" + m.getAmount() + "失败,取消冻结金额" + m.getAmount());
        this.accountFlowMapper.insert(flow);
    }

}
